import sys
import os

# 获取项目的根目录（假设当前文件在项目的子目录中）
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", ".."))
sys.path.append(project_root)


import json

import scrapy

from core.utils.cookie_format import CookieFormat
from service.analysis_response_service.analysis_response_service import AnalysisResponseService
from service.analysis_response_service.analysis_structure_service import AnalysisStructureService
from service.list_service.list_service.items import ListServiceItem


class ListCrawlSpider(scrapy.Spider):
    name = "list_crawl"

    def start_requests(self):

        fr = open("../../../page/page.txt", mode="r", encoding="utf-8")
        urls = fr.readlines()
        cookie = '__bid_n=18506e5a9e599cb9ee4207; FEID=v10-dc262d1829fd8408e13722a9b2a0031d42af33d8; __xaf_fpstarttimer__=1671546640332; __xaf_thstime__=1671546640573; __xaf_fptokentimer__=1671546640582; _ntes_nuid=1b31c776fa7481ab50d082d051915746; FPTOKEN=JtDP/eCljPt5sWlk67KesMWRgXEc46Z+HcmmV1Zed6D5VUo9rV/CYC9KZkrooGAtqlyws5ISk3jBqwfYRLb5H8c5HUGXqAwwp2aOLX7XG/GgzZsqzX6rsmfGQK0EnDeb1C597B4YjtRm3gpaFoKA4gz/9ZLSvM5tuMA8Ba45y+Uruf+MqLGL+8KlVDGOym2hm15sdEdYmcqaEztlCo2JnfyHu3Qay/WATy6tUPomVEqEc66tGOOSlT7CmEHL6oSOz5zNZwvM4eH+ySIB42y82gEAFbeh4lHuvPdCBSv0HTvMN85CyWuhnRb0Dm5FYSRmtXqB05aasl/0GK7xPMBefpmo3vDFHtYFpb7Kh5eaUW6nYqOuys6QctZ7rSS+XpWV0FZ9r6ZyVmhRc95hQ+kCVg==|CTnwWKt3w3olValPhxsN/ABbmtIgxIT6jzbIDxf+Gno=|10|38ca9ca921837f64ea75d51b28150d93; _ntes_nnid=1b31c776fa7481ab50d082d051915746,1716195259054; pver_n_f_l_n3=a; P_INFO=15160239910|1740736377|0|163|00&99|null&null&null#fuj&350200#10#0#0|&0||15160239910; NTES_CMT_USER_INFO=268819932%7C%E5%90%AC%E9%A3%8E%E8%B5%8F%E9%9B%A801%7Chttp%3A%2F%2Fmobilepics.ws.126.net%2Fnetease_subject%2F011371730d0bce2a36d4450abf4f81341551276132528177%7Cfalse%7CeWQuNTYwMmNmYjBiNWFkNDg5NGJAMTYzLmNvbQ%3D%3D; _ntes_origin_from=; UserProvince=%u5168%u56FD; ne_analysis_trace_id=1740807078586; s_n_f_l_n3=171a4b22727ad6781740807078588; _antanalysis_s_id=1740807079170; Hm_lvt_f8682ef0d24236cab0e9148c7b64de8a=1738334206,1740736311,1740807083; Hm_lpvt_f8682ef0d24236cab0e9148c7b64de8a=1740807083; pgr_n_f_l_n3=171a4b22727ad67817408070785864884; vinfo_n_f_l_n3=171a4b22727ad678.1.64.1726122968277.1740744961007.1740807947070'
        cookies = CookieFormat.cookie_to_dick(cookie)
        for page in urls:
            data = json.loads(page)
            yield scrapy.Request(url=data['url'], cookies=cookies, callback=self.parse, cb_kwargs={'news_type': data['type'], 'marker': data['marker']})

    def parse(self, response, news_type, marker):
        """
        解析响应
        :param response: 返回的结果
        :return:
        """
        # print(response.text)


        item = ListServiceItem()
        if marker == "first_response":
            item['data_list'] = AnalysisResponseService.analysis_list_first_response(response, news_type)
            # print(item['data_list'])
            yield item

        if marker == "first_structure":
            item['data_list'] = AnalysisStructureService.analysis_list_first_structure(response, news_type)
            yield item
